/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.embed.domain.service;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.embed.domain.feign.dto.QueryTemplateResp;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Objects;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
@Slf4j
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class TemplateSdkService {

    @Resource
    private DataCenterService dataCenterService;

    /**
     * 获取文本模板
     *
     * @param code 编号
     * @return 文本模板内容
     * @throws Exception 异常
     */
    public String getTextTemplate(String code) throws Exception {
        QueryTemplateResp queryTemplateResp = dataCenterService.querySingleTemplate(code);
        if(Objects.nonNull(queryTemplateResp)) {
            return queryTemplateResp.getContent();
        }
        return null;
    }

    /**
     * 获取文件模板
     *
     * @param code 编号
     * @return 文件模板输入流
     * @throws Exception 异常
     */
    public InputStream getFileTemplate(String code) throws Exception {
        QueryTemplateResp queryTemplateResp = dataCenterService.querySingleTemplate(code);
        if(Objects.nonNull(queryTemplateResp)) {
            String fileStoreName = queryTemplateResp.getFileStoreName();
            return dataCenterService.downloadTemplateFile(fileStoreName);
        }
        return null;
    }
}
